package ace.oauth2.web.controller.rpc.impl;

import ace.fw.model.R;
import ace.oauth2.model.dto.OAuth2TokenDto;
import ace.oauth2.model.request.*;
import ace.oauth2.model.response.OAuth2CheckAccessTokenResponse;
import ace.oauth2.web.biz.OAuth2Biz;
import ace.oauth2.web.controller.rpc.OAuth2Controller;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.oauth2.provider.endpoint.CheckTokenEndpoint;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author Caspar
 * @contract 279397942@qq.com
 * @create 2020/10/26
 * @description
 */
@Slf4j
@RestController
@AllArgsConstructor
public class OAuth2ControllerImpl implements OAuth2Controller {

    private OAuth2Biz oAuth2Biz;

    @SneakyThrows
    @Override
    public R<OAuth2TokenDto> createToken(@Valid OAuth2CreateTokenRequest request) {
        return oAuth2Biz.createToken(request);
    }

    @SneakyThrows
    @Override
    public R<OAuth2TokenDto> refreshToken(@Valid OAuth2RefreshTokenRequest request) {
        return oAuth2Biz.refreshToken(request);
    }

    /**
     * createToken and refreshToken method
     *
     * @param request
     * @return
     */

    @Override
    public R<Boolean> removeToken(@Valid OAuth2RemoveTokenRequest request) {
        return oAuth2Biz.removeToken(request);
    }

    /**
     * 封装 {@link CheckTokenEndpoint#checkToken(String)}
     *
     * @param request
     * @return
     */
    @Override
    public R<OAuth2CheckAccessTokenResponse> checkAccessToken(@Valid OAuth2CheckAccessTokenRequest request) {
        return oAuth2Biz.checkAccessToken(request);
    }

    @Override
    public R<Boolean> removeAllTokenByAccountId(@Valid OAuth2RemoveAllTokenByAccountIdRequest request) {
        return oAuth2Biz.removeAllTokenByAccountId(request);
    }
}
